package cn.stronglink.assetmanage.business.inventory.view.sync;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryListAdapter;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryOrderAdapter;
import cn.stronglink.assetmanage.business.inventory.model.InventoryCountModel;
import cn.stronglink.assetmanage.business.inventory.model.InventorySyncModel;
import cn.stronglink.assetmanage.business.inventory.presenter.InventoryCountPresenter;
import cn.stronglink.assetmanage.business.inventory.presenter.InventorySyncPresenter;
import cn.stronglink.assetmanage.business.inventory.view.IRefreshList;
import cn.stronglink.assetmanage.business.inventory.view.count.InventoryCountView;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.index.base.BaseFragment;
import cn.stronglink.assetmanage.index.base.BasePresenter;
import cn.stronglink.assetmanage.index.presenter.MenuPresenter;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.widget.ViewPagerFragment;

/**
 * 待同步表单列表
 */
public class InventorySyncFragment extends ViewPagerFragment implements View.OnClickListener {
    private Button btn_inventory_sync;
    private RecyclerView rv_inventorysync_order_list;
    private ExpandableListView rv_inventorysync_list;
    private InventoryListAdapter listAdapter;
    private InventoryOrderAdapter orderAdapter;
    private InventorySyncModel inventorySyncModel;
    private InventorySyncView inventorySyncView;
    private InventorySyncPresenter inventorySyncPresenter;
    private List<String> groupNameList;
    private LinearLayoutManager layoutmanager;
    private TextView txt_main_title;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_inventory_sync, container, false);
        try {
            btn_inventory_sync = (Button) rootView.findViewById(R.id.btn_inventory_sync);
            btn_inventory_sync.setOnClickListener(this);
            initViewAndData(rootView);
        }catch (Exception e){
            Toast.makeText(this.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
        return rootView;
    }

    private void initViewAndData(View view) {
        txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
        txt_main_title.setText("盘点管理");
        rv_inventorysync_order_list = view.findViewById(R.id.rv_inventorysync_order_list);
        rv_inventorysync_list = view.findViewById(R.id.rv_inventorysync_list);
        listAdapter = new InventoryListAdapter(getActivity(),null);
        groupNameList = new ArrayList<>();
        groupNameList.add(getResources().getString(R.string.inventory_more));
        groupNameList.add(getResources().getString(R.string.inventory_normal));
        orderAdapter = new InventoryOrderAdapter(getActivity(), groupNameList);
        layoutmanager =  new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        rv_inventorysync_order_list.setLayoutManager(layoutmanager);
        rv_inventorysync_order_list.setAdapter(listAdapter);
        rv_inventorysync_list.setAdapter(orderAdapter);
        listAdapter.setOnItemClickListener(new onRecyclerListener() {
            @Override
            public void onItemClicked(View v, int postion) {
                inventorySyncPresenter.showOrderByPosition(postion);
            }
        });
        rv_inventorysync_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String groupName = groupNameList.get(groupPosition);
                inventorySyncPresenter.showDetailByPosition(groupName, childPosition);
                return false;
            }
        });
        inventorySyncModel = new InventorySyncModel();
        inventorySyncView = new InventorySyncView(this, view, listAdapter, orderAdapter);
        inventorySyncPresenter = new InventorySyncPresenter(inventorySyncModel, inventorySyncView);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        try {
            if(isVisible) {
                inventorySyncPresenter.initInventoryListView();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        try {
            inventorySyncPresenter.syncInventoryData();

            //todo 获取盘点所有数据，清除盘点本地数据库表，保存获取到的数据
        }catch (Exception e){
            Toast.makeText(this.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
}
